// eslint-disable-next-line strict
const Service = require('egg').Service;

class ArticleService extends Service {
  async create(params) {
    const { app } = this;
    try {
      const result = await app.mysql.insert('article', params);
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  async updatePost(params) {
    const { app } = this;
    const { id } = params;
    const options = {
      where: {
        id,
      },
    };
    try {
      const result = await app.mysql.update('article', params, options);
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  async lists() {
    const { app } = this;
    try {
      const result = await app.mysql.select('article');
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  async detail(id) {
    if (!id) {
      console.log('id 必须有值');
      return null;
    }
    try {
      const result = await this.app.mysql.get('article', { id });
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  async posts(columnId) {
    if (!columnId) {
      console.log('columnId 必须有值');
      return null;
    }
    try {
      const result = await this.app.mysql.select('article', {
        where: {
          columnId,
        },
      });
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  async postDetail(id) {
    if (!id) {
      console.log('id 必须有值');
      return null;
    }
    try {
      const result = this.app.mysql.get('article', { id });
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  async deletePost(id) {
    if (!id) {
      console.log('id 必须有值');
      return null;
    }
    try {
      const result = this.app.mysql.delete('article', { id });
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
}

module.exports = ArticleService;
